﻿/*  ------------------------------------------------------------------------
	 NQ Common Services Library
	 
	 Homepage: http://www.awzhome.de/
	 ------------------------------------------------------------------------
	 
	 The contents of this file are subject to the Mozilla Public License
	 Version 1.1 (the "License"); you may not use this file except in
	 compliance with the License. You may obtain a copy of the License at
	 http://www.mozilla.org/MPL/
	 
	 Software distributed under the License is distributed on an "AS IS"
	 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	 License for the specific language governing rights and limitations
	 under the License.
	
	 The Original Code is code of NQ Common Services Library.

	 The Initial Developer of the Original Code is Andreas Weizel.
	 Portions created by the Initial Developer are
	 Copyright (C) 2011 Andreas Weizel. All Rights Reserved.
	 
	 Contributor(s): (none)
	 
	 Alternatively, the contents of this file may be used under the terms
	 of the GNU General Public License Version 2.0 or later (the "GPL"),
	 or the GNU Lesser General Public License Version 2.1 or later (the
	 "LGPL"), in which case the provisions of the GPL or the LGPL are
	 applicable instead of those above. If you wish to allow use of your
	 version of this file only under the terms of the GPL or the LGPL and
	 not to allow others to use your version of this file under the MPL,
	 indicate your decision by deleting the provisions above and replace
	 them with the notice and other provisions required by the GPL or the
	 LGPL. If you do not delete the provisions above, a recipient may use
	 your version of this file under the terms of any of the MPL, the GPL
	 or the LGPL.
	 ------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWZhome.NQ.Core.Fluent;
using AWZhome.NQ.Core;

namespace AWZhome.NQ.CommonServices.Fluent
{
	/// <summary>
	/// Wrapper class for a configuration list in configuration fluent interface.
	/// </summary>
	public class ConfigurationListWrapper
	{
		private string _listName;
		private string _categoryName;
		private string _groupName;
		IConfigManager _configService;

		/// <summary>
		/// Class constructor.
		/// </summary>
		public ConfigurationListWrapper(string listName)
		{
			try
			{
				_configService = ServiceCore
					.Get<Services.IConfigService>()
					.To;
				_categoryName = null;
				_groupName = null;
				_listName = listName;
			}
			catch (NQServiceLoadingException)
			{
				// There seems to be no ConfigService, ignore this
			}
		}

		/// <summary>
		/// Sets the category name, where the setting is found in.
		/// </summary>
		/// <param name="name">Category name.</param>
		/// <returns>Instance of the wrapper.</returns>
		public ConfigurationListWrapper OfCategory(string name)
		{
			_categoryName = name;
			return this;
		}

		/// <summary>
		/// Selects a setting from a component group.
		/// </summary>
		/// <returns>Instance of the wrapper.</returns>
		public ConfigurationListWrapper OfComponents()
		{
			_categoryName = "components";
			return this;
		}

		/// <summary>
		/// Selects a setting from a container group.
		/// </summary>
		/// <returns>Instance of the wrapper.</returns>
		public ConfigurationListWrapper OfContainers()
		{
			_categoryName = "containers";
			return this;
		}

		/// <summary>
		/// Sets the group name, where the setting is found in.
		/// </summary>
		/// <param name="name">Group name.</param>
		/// <returns>Instance of the wrapper.</returns>
		public ConfigurationListWrapper InGroup(string name)
		{
			_groupName = name;
			return this;
		}

		/// <summary>
		/// Sets the list contents of the given setting in user-specific configuration.
		/// </summary>
		/// <param name="data">Data list to assign.</param>
		/// <returns><c>True</c>, if successful, <c>false</c> otherwise.</returns>
		public bool Set(IList<ConfigData> data)
		{
			if (_configService != null)
			{
				return _configService.SetListSetting(_categoryName, _groupName, _listName, data, true);
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// Sets the list contents of the given seting in global configuration.
		/// </summary>
		/// <param name="data">Data list to assign.</param>
		/// <returns><c>True</c>, if successful, <c>false</c> otherwise.</returns>
		public bool SetGlobal(IList<ConfigData> data)
		{
			if (_configService != null)
			{
				return _configService.SetListSetting(_categoryName, _groupName, _listName, data, false);
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// Removes the given list setting from user-specific configuration.
		/// </summary>
		/// <returns><c>True</c>, if successful, <c>false</c> otherwise.</returns>
		public bool Remove()
		{
			if (_configService != null)
			{
				return _configService.RemoveListSetting(_categoryName, _groupName, _listName, true);
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// Removes the given list setting from global configuration.
		/// </summary>
		/// <returns><c>True</c>, if successful, <c>false</c> otherwise.</returns>
		public bool RemoveGlobal()
		{
			if (_configService != null)
			{
				return _configService.RemoveListSetting(_categoryName, _groupName, _listName, false);
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// Returns the contents of the given list setting as
		/// <see cref="System.Collections.Generic.IList{AWZhome.NQ.CommonServices.NQPersistDataBasic}"/>.
		/// </summary>
		public IList<ConfigData> Get
		{
			get
			{
				if (_configService != null)
				{
					return _configService.GetListSetting(_categoryName, _groupName, _listName);
				}
				else
				{
					return null;
				}
			}
		}
	}
}
